2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
8 namespace SuperPolarity
10 static class ActorManager
13 static SuperPolarity Game;
14 static int OutlierBounds;
15 static IList<Actor> Actors;
20 Actors = new List<Actor>();
23 static public void CheckIn(Actor actor)
28 static public void CheckOut(Actor actor)
33 static public void Update(GameTime gameTime)
37 foreach (Actor actor in Actors)
39 actor.Update(gameTime);
43 static public void Draw(SpriteBatch spriteBatch)
45 foreach (Actor actor in Actors)
47 actor.Draw(spriteBatch);
51 static void CheckActors()
53 for (var i = Actors.Count - 1; i >= 0; i--)
55 if (i >= Actors.Count) {
58 Actor actor = Actors[i];
59 for (var j = i - 1; j >= 0; j--)
61 Actor other = Actors[j];
63 CheckCollision(actor, other);
65 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
67 CheckMagnetism((Ship)actor, (Ship)other);
73 static void CheckCollision(Actor actor, Actor other)
75 var collision = Rectangle.Intersect(actor.Box, other.Box);
76 var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
77 if (!collision.IsEmpty && !actor.Dying && !other.Dying)
79 actor.Collide(other, collision);
80 other.Collide(actor, inverseCollision);
85 static void CheckMagnetism(Ship actor, Ship other)
87 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
89 var dy = other.Position.Y - actor.Position.Y;
90 var dx = other.Position.X - actor.Position.X;
91 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
92 var angle = (float) Math.Atan2(dy, dx);
93 var otherAngle = (float)Math.Atan2(-dy, -dx);
95 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
97 actor.Magnetize(other, (float)linearDistance, angle);
98 other.Magnetize(actor, (float)linearDistance, otherAngle);
103 static void CheckOutliers()
105 for (var i = Actors.Count; i > 0; i--)
107 var actor = Actors[i-1];
108 if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
109 actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
110 actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
117 internal static void SetGame(SuperPolarity game)